********************************************************************************
* Input files: data_FamilyTree: ID:s of parents and grand parents for each 
* individual
* Output file: A list of relatives containing  siblings and cousins
********************************************************************************

clear all
use  "Data\data_FamilyTree", replace
sort PersonId

* Reshape data to have each PersonId on multiple rows with Ids of all relatives
* in a second column
rename MorId Relative1
rename FarId Relative2
rename MorMorId Relative3
rename MorFarId Relative4
rename FarMorId Relative5
rename FarFarId Relative6
reshape long Relative, i(PersonId) j(Rel)
drop Rel
drop if Relative == .

* "Joinby" in order to make a list of all relatives,
* i.e. persons that share a parents or grandparent
* Make a copy of the file with individuals and create all pairs of PersonId and
* PersonId2 which have a parents or grandparend in common
rename PersonId PersonId2
save Data/tmp_relatives, replace
use Data/tmp_relatives, clear
rename PersonId2 PersonId  

joinby Relative using Data/tmp_relatives
drop Relative

* There will be duplicates because, e.g., those with the same mother is counted
* once and those with the same fathers is counted once. Cases showing that
* individuals are relatives of themselves are also created.
* Drop these duplicates
duplicates drop
sort PersonId
drop if PersonId == PersonId2
rename PersonId2 Relative

save Data/data_relatives, replace
